<?php
namespace wlcat\src;
use wlcat\http\MiddleWare\MiddleFrameWareInterface;

class MiddleWare {
    static public function load() {
        $kernels = include_once FRAME_PATH.'http/Kernel.php';
        $request = Register::get(Register::REQUEST_CLASS);
        if (!is_array($kernels))
        {
            return false;
        }
        foreach ($kernels as $kernel)
        {
            if (!empty($kernel))
            {
                if (class_exists($kernel))
                {
                    $middleWare = new $kernel();
                    if (! $middleWare instanceof MiddleFrameWareInterface)
                    {
                        exit('中间件: '.$kernel." 没有实现接口 \\wlcat\\http\\MiddleWare\\MiddleFrameWareInterface");
                    }
                    $middleWare->handle($request, function(Request $request) {
                        Register::set(Register::REQUEST_CLASS, $request);
                    });
                } else {
                    exit('中间件不存在: '.$kernel);
                }

            }
        }
    }
}